package com.ibm.cloud.redismq.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import com.ibm.cloud.redismq.conf.RedisMQProperties;
public abstract class MessageWorkerBase implements Runnable{
	Logger log = LoggerFactory.getLogger(getClass());
	@Autowired protected RedisConnectionFactory connFactory;
	@Autowired protected RedisMQProperties properties;
	protected boolean started = true;
	protected RedisConnection connection;

	public void setStarted(boolean started) {
		this.started = started;
	}
	abstract protected void init();
	protected void finish() {}
	public void run() {
		log.info("{}-{} started.", Thread.currentThread().getName());
		this.started = true;
		this.connection = connFactory.getConnection();
		init();
		// loop while the parent is started
		while (started) {
			loopedWork();
		}
		finish();
		log.info("{} exited. ", Thread.currentThread().getName());

	}
	protected abstract void loopedWork();
	
}
